package com.example.tys.baseapplication.util;
/*
* Copyright 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;

import com.example.tys.baseapplication.MainActivity;
import com.example.tys.baseapplication.R;

/**
 * Helper class to manage notification channels, and create notifications.
 *
 * @author tys
 */
public class NotificationHelper extends ContextWrapper {
    private NotificationManager manager;
    private static int NOTIFY_ID = 0xA000;

    /**
     * Registers notification channels, which can be used later by individual notifications.
     *
     * @param ctx The application context
     */
    public NotificationHelper(Context ctx) {
        super(ctx);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            for (NotifyType notifyType : NotifyType.values()) {
                NotificationChannel chan1 = new NotificationChannel(notifyType.primaryChannel,
                        notifyType.nameChannel, notifyType.importanceChannel);
                chan1.setLightColor(Color.GREEN);
                chan1.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
                //模拟器 右上角点，长按显示title body(在真机上没有)
                chan1.setShowBadge(false);
                getManager().createNotificationChannel(chan1);
            }
        }
    }


    /**
     * Get a notification of type 1
     * <p>
     * Provide the builder rather than the notification it's self as useful for making notification
     * changes.
     *
     * @param title the title of the notification
     * @param body  the body text for the notification
     * @return the builder as it keeps a reference to the notification (since API 24)
     */
    public NotificationCompat.Builder getNotification(String title, String body, NotifyType type) {
        return new NotificationCompat.Builder(getApplicationContext(), type.getPrimaryChannel())
                .setContentTitle(title)
                .setContentText(body)
                .setSmallIcon(getSmallIcon())
                .setContentIntent(getNotifyIntent(title, body, type))
                .setAutoCancel(true);
    }


    /**
     * Send a notification.
     *
     * @param id           The ID of the notification
     * @param notification The notification object
     */
    public void notify(int id, NotificationCompat.Builder notification) {
        getManager().notify(id, notification.build());
    }

    /**
     * Get the small icon for this app
     *
     * @return The small icon resource id
     */
    private int getSmallIcon() {
        return R.mipmap.ic_launcher;
    }

    /**
     * Get the notification manager.
     * <p>
     * Utility method as this helper works with it a lot.
     *
     * @return The system service NotificationManager
     */
    private NotificationManager getManager() {
        if (manager == null) {
            manager = (NotificationManager) getApplicationContext().getSystemService(
                    Context.NOTIFICATION_SERVICE);
        }
        return manager;
    }

    /**
     * 每条通知栏点击事件
     * 如果有多条通知，并且处于合并状态时。点击事件会再次打开应用(启动MAIN Activity，即使应用已运行)
     * 栈顶只允许一个，已经在栈顶{@link android.app.Activity#onNewIntent}非栈顶(不存在){@link android.app.Activity#onCreate(Bundle)}
     * {@link Intent#FLAG_ACTIVITY_SINGLE_TOP}  singleTop
     * 栈中只允许一个，如果不存在当前栈中创建此Activity实例，存在则把栈中其之上的所以Activity实例销毁，并调用此实例中的{@link android.app.Activity#onNewIntent}方法
     * {@link Intent#FLAG_ACTIVITY_CLEAR_TOP}|{@link Intent#FLAG_ACTIVITY_SINGLE_TOP} singleTask
     *
     * @param title
     * @param body
     * @param type
     * @return
     */
    private PendingIntent getNotifyIntent(final String title, final String body, final NotifyType type) {
        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP |
                Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.putExtra("title", title);
        intent.putExtra("body", body);
        return PendingIntent.getActivity(getApplicationContext(), NOTIFY_ID++, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
    }


    public enum NotifyType {

        /**
         * 无声音，无振动 下拉后才能看到
         */
        MIN_TYPE("min", "n_min", 1),

        LOW_TYPE("low", "n_low", 2),

        /**
         * 声音 振动
         */
        NORMAL_TYPE("default", "n_default", 3),

        HIGH_TYPE("height", "n_high", 4),

        MAX_TYPE("max", "n_max", 5);


        NotifyType(String primary, String name, int importanceDefault) {
            this.primaryChannel = primary;
            this.nameChannel = name;
            this.importanceChannel = importanceDefault;
        }

        private String primaryChannel;
        private String nameChannel;
        private int importanceChannel;

        public String getPrimaryChannel() {
            return primaryChannel;
        }

        public String getNameChannel() {
            return nameChannel;
        }

        public int getImportanceChannel() {
            return importanceChannel;
        }
    }

}